package com.yunhe.option.controller.base;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.option.domain.base.Item;
import com.yunhe.option.service.base.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ItemController
 * @Description TODO
 * @Author HeTao
 * @Date 2021/2/26 14:19
 * @Version 1.0
 **/
@Api(value = "ItemController", description = "条目", tags = {"Base_Item"})
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    /**
     * 新增枚举
     * @param item 条目对象
     */
    @ApiOperation("新增条目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enumId", value = "枚举id", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "parentId", value = "父级id", required = false, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/{enumId}",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> post(@PathVariable("enumId") Long enumId,
                                             @RequestParam(value = "parentId", required = false) Long parentId,
                                             @RequestBody Item item)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        Item newItem = itemService.createItem(enumId,parentId,item);
        return new ResponseEntity<>(new ResultObject<>(newItem), HttpStatus.OK);
    }

    @ApiOperation("编辑条目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "条目id", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public ResponseEntity<ResultObject> updateEnum(@PathVariable("id") Long id,
                                                   @RequestBody Item item)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        item.setId(id);
        Item newItem = itemService.updateItem(item);
        return new ResponseEntity<>(new ResultObject<>(newItem), HttpStatus.OK);
    }

    @ApiOperation("删除条目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "条目id", required = true, dataType = "long", paramType = "path"),
    })
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id)
            throws ArgumentErrorException {
        itemService.delete(id);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }


    @ApiOperation("查看枚举详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "条目id", required = true, dataType = "long", paramType = "path"),
    })
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getItemById(@PathVariable("id") Long id) throws ObjectNotFoundException {
        Item item = itemService.getItemById(id);
        return new ResponseEntity<>(new ResultObject<>(item), HttpStatus.OK);
    }

    @ApiOperation("通过枚举编码查询第一层条目数据数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enumName", value = "枚举编码", required = true, dataType = "String", paramType = "path"),
    })
    @RequestMapping(value = "/{enumName}/getItemByEnumName",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getItemByEnumName(@PathVariable("enumName") String enumName) throws ArgumentErrorException {
        Iterable<Item> items = itemService.getItemByEnumName(enumName);
        return new ResponseEntity<>(new ResultObject<>(items), HttpStatus.OK);
    }

    @ApiOperation("通过枚举编码查询树形条目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enumName", value = "枚举编码", required = true, dataType = "String", paramType = "path"),
    })
    @RequestMapping(value = "/getAllItems/{enumName}",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getAllItems(@PathVariable("enumName") String enumName) throws ArgumentErrorException {
        Iterable<Item> items = itemService.getAllItems(enumName);
        return new ResponseEntity<>(new ResultObject<>(items), HttpStatus.OK);
    }

    @ApiOperation("通过条目id查询子条目数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "父级id", required = true, dataType = "Long", paramType = "path"),
    })
    @RequestMapping(value = "/{id}/getChildItemsById",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getChildItemsById(@PathVariable("id") Long id) throws ArgumentErrorException {
        Iterable<Item> items = itemService.getChildItemsById(id);
        return new ResponseEntity<>(new ResultObject<>(items), HttpStatus.OK);
    }

    @ApiOperation("通过条目code以及枚举name查询条目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "枚举name", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/getItemByItemCodeAndEnumName",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getItemByItemCodeAndEnumName(@RequestParam(value = "name",required = true) String name) throws ArgumentErrorException {
        Iterable<Item> items = itemService.getItemByItemCodeAndEnumName(name);
        return new ResponseEntity<>(new ResultObject<>(items), HttpStatus.OK);
    }

}
